package com.applet.addressbook.service.impl;

import com.applet.addressbook.mapper.AddressBookMapper;
import com.applet.addressbook.service.AddressBookService;
import com.applet.constant.AddressConstant;
import com.applet.context.AlibabaTTL;
import com.applet.entity.AddressBook;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 地址簿 serviceImpl
 */
@Slf4j
@Service
public class AddressBookServiceImpl implements AddressBookService {


    @Resource
    private AddressBookMapper addressBookMapper;



    /**
     * 地址簿管理
     *         新增地址簿
     * @param addressBook
     */
    @Override
    public void addUserAddressBook(AddressBook addressBook){
        // 用户id兜底
        if(Objects.isNull(addressBook.getUserId())){
            Long userId = Long.valueOf(AlibabaTTL.threadLocalGet().toString());
            addressBook.setUserId(userId);
        }
        // 新增地址簿 不是默认的 0
        addressBook.setIsDefault(AddressConstant.ADDRESSBOOK_NOT_IS_DEFAULT);
        Integer count = addressBookMapper.insert(addressBook);
        log.info("用户id: {},新增地址簿，影响行数: {}", AlibabaTTL.threadLocalGet(), count);
    }



    /**
     * 地址簿管理
     *         查询当前用户名下收货地址列表
     * @param userId
     */
    @Override
    public List<AddressBook> selectUserAddressBookList(Long userId) {
        // 兜底
        if(Objects.isNull(userId)){
            userId = Long.valueOf(AlibabaTTL.threadLocalGet().toString());
        }
        return addressBookMapper.getUserAddressBookList(userId);

    }



    /**
     * 地址簿管理
     *          查询用户名下默认的地址簿
     * @param userId
     */
    @Override
    public AddressBook getDefault(Long userId) {
        // 兜底
        if(Objects.isNull(userId)){
            userId = Long.valueOf(AlibabaTTL.threadLocalGet().toString());
        }
        AddressBook addressBook = AddressBook.builder().userId(userId).isDefault(AddressConstant.ADDRESSBOOK_IS_DEFAULT).build();
        return addressBookMapper.selectDefaultAddressBook(addressBook);
    }


    /**
     * 地址簿管理
     *         设置用户名下默认地址
     * @param addressBook
     */
    @Override
    public void setDefault(AddressBook addressBook) {

        // 1.将当前用户名下所有收货地址设置为非默认
        //SQL: update address_book set is_default = ? where user_id =?        //多条
        Long userId = Long.valueOf(AlibabaTTL.threadLocalGet().toString());
        AddressBook allBook = AddressBook.builder().userId(userId).isDefault(AddressConstant.ADDRESSBOOK_NOT_IS_DEFAULT).build();
        Integer zeroCount = addressBookMapper.updateUserAddressBookAllIsZero(allBook);

        // 2.设置当前传递过来的地址簿为默认
        //SQL; update address_book set is_default = ? where id = ?            //单条
        addressBook.setIsDefault(AddressConstant.ADDRESSBOOK_IS_DEFAULT);
        Integer count = addressBookMapper.updateById(addressBook);
        log.info("用户id: {},修改地址簿id:{} 为默认地址簿,影响行数: {}", userId, addressBook.getId(), count);
    }



    /**
     * 地址簿管理
     *          根据id查询地址簿信息
     * @param id
     */
    @Override
    public AddressBook selectAddressBookById(Long id) {
        return addressBookMapper.getById(id);
    }



    /**
     * 地址簿管理
     *          根据id修改地址簿
     * @param addressBook
     */
    @Override
    public void updateAddressBook(AddressBook addressBook) {
        Integer count = addressBookMapper.updateById(addressBook);
        log.info("地址簿id: {},修改地址簿影响行数: {}", addressBook.getId(), count);
    }



    /**
     * 地址簿管理
     *         根据id删除地址簿
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        Integer count =  addressBookMapper.deleteById(id);
        log.info("地址簿id: {},删除影响行数: {}", id, count);
    }


}
